package com.wyh.lotterysystem.controller;

import com.wyh.lotterysystem.common.errorcode.ControllerErrorCodeConstants;
import com.wyh.lotterysystem.common.exception.ControllerException;
import com.wyh.lotterysystem.common.pojo.CommonResult;
import com.wyh.lotterysystem.controller.request.PasswordLoginRequest;
import com.wyh.lotterysystem.controller.request.RegisterRequest;
import com.wyh.lotterysystem.controller.request.VerificationCodeLoginRequest;
import com.wyh.lotterysystem.controller.response.LoginResponse;
import com.wyh.lotterysystem.controller.response.RegisterResponse;
import com.wyh.lotterysystem.service.UserService;
import com.wyh.lotterysystem.service.dto.LoginDTO;
import com.wyh.lotterysystem.service.dto.RegisterDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    /**
     * 用户注册
     * @param request
     * @return
     */
    @RequestMapping("/register")
    public CommonResult<RegisterResponse> register(@RequestBody @Validated RegisterRequest request){
        //打印日志
        log.error("用户注册，user{}", request.toString());

        //构造service层请求
        RegisterDTO registerDTO = userService.register(request);

        //构造返回值
        RegisterResponse response = new RegisterResponse();
        response.setId(registerDTO.getId());
        CommonResult<RegisterResponse> result = CommonResult.success(response);

        return result;
    }

    /**
     * 用户登录 —— 用户名 + 密码
     * @param request
     * @return
     */
    @RequestMapping("/loginByPassword")
    public CommonResult<LoginResponse> loginByPassword(@RequestBody @Validated PasswordLoginRequest request){
        //打印日志
        log.info("用户登录，user{}",request.getLoginName());

        //调用service层
        LoginDTO loginDTO = userService.userLogin(request);

        //构造返回对象
        LoginResponse response = parsePasswordLoginResponse(loginDTO);

        //返回
        return CommonResult.success(response);
    }

    /**
     * 登录 —— qq邮箱号 + 验证码
     * @param request
     * @return
     */
    @RequestMapping("/loginByMessage")
    public CommonResult<LoginResponse> loginByMessage(@RequestBody @Validated VerificationCodeLoginRequest request){
        //打印日志
        log.info("用户登录，user{}",request.getLoginMobile());

        //调用Service层
        LoginDTO loginDTO = userService.userLogin(request);

        //构造返回对象
        LoginResponse response = parsePasswordLoginResponse(loginDTO);

        //返回
        return CommonResult.success(response);
    }

    /**
     * LoginDTO转LoginResponse
     * @param loginDTO Service返回的参数
     * @return
     */
    private LoginResponse parsePasswordLoginResponse(LoginDTO loginDTO) {
        //校验参数
        if (null == loginDTO){
            throw new ControllerException(ControllerErrorCodeConstants.LOGIN_ERROR);
        }

        LoginResponse response = new LoginResponse();
        response.setToken(loginDTO.getToken());
        response.setIdentity(loginDTO.getIdentity());

        return response;
    }

    /**
     * 发送验证码
     */
    @RequestMapping("/sendVerificationCode")
    public CommonResult<Boolean> sendVerificationCode(String email){
        //打印日志
        log.info("验证码发送......");

        //调用Service层
        userService.sendVerificationCode(email);

        //返回
        return CommonResult.success(Boolean.TRUE);
    }
}
